package com.example.demo.controller;

import com.example.demo.model.RestResponseBo;
import com.example.demo.model.StaticFields;
import com.example.demo.model.dto.UserDto;
import com.example.demo.model.po.UserPo;
import com.example.demo.service.ManageService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * create by elitetyc
 * date：2019/6/12
 * desc：
 **/

@Controller
@RequestMapping("/admin")
public class ManageController {

    @Autowired
    private ManageService manageService;

    @GetMapping(value = "/index/{page}")
    public String index( @PathVariable int page,HttpServletRequest request) {
        PageInfo<UserPo> userDtoPageInfo = manageService.selectAllUser(page);
        request.setAttribute("users",userDtoPageInfo);
        return "index";
    }


    @GetMapping(value = "/add")
    public String add() {
        return "add";
    }


    @GetMapping(value = "/setting")
    public String setting(HttpServletRequest request) {
        UserDto userDto = (UserDto) request.getSession().getAttribute(StaticFields.LOGIN_SESSION_KEY);
        request.setAttribute("userinfo",userDto);
        return "setting";
    }


    @PostMapping("/setting")
    @ResponseBody
    public RestResponseBo settings(@RequestParam String name,
                                   @RequestParam int age,
                                   @RequestParam int gender,
                                   @RequestParam String addr,
                                   @RequestParam String phone,
                                   HttpServletRequest request){

        UserDto userDto = (UserDto) request.getSession().getAttribute(StaticFields.LOGIN_SESSION_KEY);
        if (userDto==null){
            return RestResponseBo.fail("请先登录");
        }
        UserPo userPo = new UserPo();
        userPo.setUserId(userDto.getUserId());
        userPo.setName(name);
        userPo.setAge(age);
        userPo.setGender(gender);
        userPo.setAddr(addr);
        userPo.setPhone(phone);
        int res= manageService.updateInfo(userPo);
        if (res==1){
            UserDto userDto1 = new UserDto();
            BeanUtils.copyProperties(userPo,userDto1);
            request.getSession().setAttribute(StaticFields.LOGIN_SESSION_KEY,userDto1);
            return RestResponseBo.ok();
        }else {
            return RestResponseBo.fail("请稍后再试");
        }

    }


    @PostMapping("/deleteUser")
    @ResponseBody
    public RestResponseBo deleteUser(@RequestParam int userId){
        int res = manageService.deleteUserById(userId);
        if (res==0){
            return RestResponseBo.fail("删除失败");
        }else {
            return RestResponseBo.ok();
        }
    }


    @PostMapping("/addUser")
    @ResponseBody
    public RestResponseBo addUser(
                                @RequestParam String account,
                                @RequestParam String password,
                                @RequestParam String name,
                                  @RequestParam int age,
                                  @RequestParam int gender,
                                  @RequestParam String addr,
                                  @RequestParam String phone){
        UserPo userPo = new UserPo(0,account,password,name,age,gender,addr,phone);
        return manageService.insertUser(userPo);
    }


}
